/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package rentcalculator.persist;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import rentcalculator.model.Lodgement;
import rentcalculator.util.DateUtil;

/**
 *
 * @author developer
 */
public class LodgementDao {
    public void add(Lodgement p){
        Persist.getInstance().add(p);
        
        
    }
    
    
    public void remove(Lodgement p){
        Persist.getInstance().remove(p);
    }
    
    public void update(Lodgement p){
        Persist.getInstance().update(p);
    }
    
    
    public List<Lodgement> findAll(){
        List<Lodgement> ps;
        ps = (List<Lodgement>)Persist.getInstance().findALL(Lodgement.class);
        
        return ps;
    }
    
    public Lodgement findById(long id){
        Object r;
        Lodgement l;
        l = null;
        r = Persist.getInstance().findById(Lodgement.class, id);
        if(r!= null) l = (Lodgement)r;
        return l;
    }
    
    /**
     * Find renders who lives or lived in the property in the given time period 
     * @param propertyId
     * @param start can be null
     * @param end can be null
     * @return lodgement info
     */
    public List<Lodgement> findRentersByFacilityId(long facilityId, Date start, Date end){
        
        Map<String, String> params;
        params = new HashMap<>();
        params.put("start", DateUtil.trunc(start));
        params.put("end", DateUtil.trunc(end));
        params.put("id", String.valueOf(facilityId));
 
        List<Lodgement> ls;
        ls = Persist.getInstance().
                find(Lodgement.class, FIND_RENTERS_BY_FACILITY, params);
              
        return ls;
              
    }
    
    /**
     * Find property where the given person lives or lived during a specific time period 
     * @param personId
     * @param start can be null
     * @param end can be null
     * @return 
     */
    public List<Lodgement> findFacilityByPersonId(long personId, Date start, Date end){
        
        Map<String, String> params;
        params = new HashMap<>();
        params.put("start", DateUtil.trunc(start));
        params.put("end", DateUtil.trunc(end));
        params.put("id", String.valueOf(personId));
 
        List<Lodgement> ls;
        ls = Persist.getInstance().
                find(Lodgement.class, FIND_FACILITY_BY_PERSON, params);
              
        return ls;
              
    }
    
    private final static String FIND_RENTERS_BY_FACILITY = 
            "select * from Lodgement renters"
            + " where (:start is null or start <= :start)"
            + " and (:end is null or end >= :end)"
            + " and facility_id = :id ;";
    private final static String FIND_FACILITY_BY_PERSON = 
            "select * from Lodgement renters"
            + " where (:start is null or start <= :start)"
            + " and (:end is null or end >= :end)"
            + " and person_id = :id ;";
    
    
    
}
